<style>
	#language_cases table {
		
	}

  #language_cases td {
    padding:1px;
		margin:0px;
  }
</style>

<div id="language_cases_container" style="font-size:12px;">
  <%=render(:partial => "header", :locals => {:section => "language_cases", :label => "Language Cases", :description => "", :mode => mode})%>
  
  <%= form_for(tr8n_current_language, :as => :language, :url => {:action => :index}, :html => {:id => 'language_cases_form', :method => :post}) do |f| %>
    <div id="language_cases_content" class="section grey">
      <div class="tr8n_fltr" style="padding-right:5px;">
          <%=tr8n_help_icon_tag %>
      </div>
			
      <div>
				<div class="tr8n_txtl" style="padding-top:5px;padding-bottom:15px;font-size:12px;">
					<%=tra("A language case is an inflection or use of a noun (or pronoun) to show its relation to other words in the sentence.")%>
					<%=tra("In some languages, like Russian, the form of a token {actor} will changed based on where and how the token is used in a sentence.", "", :actor => "{actor}")%>
          <%=tra("For instance, in {example}, {actor} token will be in the genitive form of the language.", "", 
                  :example => "<span dir='ltr' style='display:inline-block;font-weight:bold;'>{target} received a gift from {actor}</span>",
                  :actor => "{actor}")%>
					<br><br>
          <%=tra("This section allows you to define the various cases of your language so that translators can provide the correct form for each dynamic value of any token.")%>
          <br><br>
          <%=tra("The translation engine evaluates rules in the order you have specified here until it finds the first rule that matches the criteria and applies that rule.")%>
          <%=tra("If you are dealing with an exceptional case for a specific word, you can add exceptions by following the link below.")%>
				</div>

        <div id="language_cases">
         <% if mode == :edit %>
            <%= render :partial => "edit_language_cases" %> 
				 <% elsif @cases.empty? %>
            <div class="tr8n_txtl" style="font-style:italic; font-size: 10px; color: grey;">
					    <%=tra("There are no language cases defined for this language.")%>
					  </div>
				<% else %>
					  <% @cases.each_with_index do |lcase, index| %>
						    <div>
							    <div class="tr8n_txtl" style="padding-bottom:5px; font-size:12px;">
                    <%=tr8n_toggler_tag("case_#{index}_content", "", false)%>
					          <strong><%= lcase.native_name %></strong>
					          <span style="color:grey; font-size:10px;">
					            <% if lcase.application == 'phrase' %>
                        <%= tra("applied to the entire phrase of the token value") -%>
					            <% else %>
                        <%= tra("applied to every word in the token value") -%>
					            <% end %>
					          </span> 
										<br>
                    <span style="color:grey; font-size:10px; padding-left:15px;" dir="ltr">(::<%= lcase.keyword %>) <%= lcase.description %> </span>
									</div>
									
									<div  class="tr8n_txtl" id="case_<%=index%>_content" style="display:none">
                    <% if lcase.rules.empty? %>
                      <div class="tr8n_txtl" style="padding:10px;padding-left:15px;color:red; font-size:10px;">
                        <%=tra("No rules have been configured for this language case") %>
                      </div>
                    <% end %>
		                <ol style="font-size: 12px; padding-left:20px; margin:1em; list-style-type:decimal; margin-top:0px;">
		                <% lcase.rules.each do |rule| %>
		                    <li class="tr8n_txtl" dir="ltr"><%=rule.description %></li>
		                <% end %> 
		                </ol>
									</div>
								</div>	
					  <% end %>
						
			      <div class="tr8n_txtl" style="padding-top:15px;">
			          <%=link_to(tra("Click here to view the exceptional case values submitted by translators"), :controller => "/tr8n/language_cases", :action => :index) %>
			      </div>
												    
					<% end %>
        </div>	
      </div>
    </div>
  <% end %>
	
</div>

<script>
  function updateLanguageCases(language_id) {
    performLanguageCasesOperation(Tr8n.Utils.serializeForm('language_cases_form'));
  }
  
  function addLanguageCase(position) {
    var form_hash = Tr8n.Utils.serializeForm('language_cases_form');
    form_hash['case_action'] = "add_at_" + position;
    performLanguageCasesOperation(form_hash);
  }

  function editLanguageCase(position) {
		Tr8n.Effects.hide("language_case_labels_" + position);
		Tr8n.Effects.hide("language_case_edit_link_" + position);
    Tr8n.Effects.show("language_case_fields_" + position);
  }

  function deleteLanguageCase(position) {
		if (!confirm("<%=tra('Are you sure you want to delete this language case?')%>")) 
		  return;
			
    var form_hash = Tr8n.Utils.serializeForm('language_cases_form');
    form_hash['case_action'] = "delete_at_" + position;
    performLanguageCasesOperation(form_hash);
  }
  
  function clearLanguageCases() {
    var form_hash = Tr8n.Utils.serializeForm('language_cases_form');
    form_hash['case_action'] = "clear_all";
    performLanguageCasesOperation(form_hash);
  } 
  
  function performLanguageCasesOperation(params){
    Tr8n.Utils.update('language_cases', '/tr8n/language/update_language_cases', {
      parameters: params,
      method: 'post'
    });
  }
	
	var edit_case_index, edit_rule_index, case_action;
  function addLanguageCaseRule(case_index, position) {
    var form_hash = {};
    form_hash['case_action'] = "add_rule_at_" + position;
    form_hash['case_index'] = case_index;
    form_hash['rule_index'] = position;

    form_hash['case[keyword]'] = Tr8n.value('cases_' + case_index + '_keyword');
    form_hash['case[native_name]'] = Tr8n.value('cases_' + case_index + '_native_name');
    form_hash['case[description]'] = Tr8n.value('cases_' + case_index + '_description');
		
		edit_case_index = case_index;
		edit_rule_index = position;
		edit_case_action = form_hash['case_action'];
		
    Tr8n.UI.Lightbox.show('/tr8n/language/lb_language_case_rule?' + Tr8n.Utils.toQueryParams(form_hash), {width:600, height:270});
  }

  function editLanguageCaseRule(case_index, position) {
    var form_hash = {};
    form_hash['case_action'] = "update_rule_at_" + position;
    form_hash['case_index'] = case_index;
    form_hash['rule_index'] = position;
		
    form_hash['case[keyword]'] = Tr8n.value('cases_' + case_index + '_keyword');
    form_hash['case[native_name]'] = Tr8n.value('cases_' + case_index + '_native_name');
    form_hash['case[description]'] = Tr8n.value('cases_' + case_index + '_description');

    form_hash['rule[definition][gender]'] = Tr8n.value('cases_' + case_index + '_rules_' + position + '_definition_gender');
    form_hash['rule[definition][part1]'] = Tr8n.value('cases_' + case_index + '_rules_' + position + '_definition_part1');
    form_hash['rule[definition][value1]'] = Tr8n.value('cases_' + case_index + '_rules_' + position + '_definition_value1');
    form_hash['rule[definition][multipart]'] = Tr8n.value('cases_' + case_index + '_rules_' + position + '_definition_multipart');
    form_hash['rule[definition][operator]'] = Tr8n.value('cases_' + case_index + '_rules_' + position + '_definition_operator');
    form_hash['rule[definition][part2]'] = Tr8n.value('cases_' + case_index + '_rules_' + position + '_definition_part2');
    form_hash['rule[definition][value2]'] = Tr8n.value('cases_' + case_index + '_rules_' + position + '_definition_value2');
    form_hash['rule[definition][operation]'] = Tr8n.value('cases_' + case_index + '_rules_' + position + '_definition_operation');
    form_hash['rule[definition][operation_value]'] = Tr8n.value('cases_' + case_index + '_rules_' + position + '_definition_operation_value');
    
    edit_case_index = case_index;
    edit_rule_index = position;
    edit_case_action = form_hash['case_action'];
    
    Tr8n.UI.Lightbox.show('/tr8n/language/lb_language_case_rule?' + Tr8n.Utils.toQueryParams(form_hash), {width:600, height:300});
  }

  function updateLanguageCaseRule(rule_hash) {
    var form_hash = Tr8n.Utils.serializeForm('language_cases_form');
    form_hash['case_index'] = edit_case_index;
    form_hash['rule_index'] = edit_rule_index;
    form_hash['case_action'] = edit_case_action;
		
		for (attrname in rule_hash) { form_hash[attrname] = rule_hash[attrname]; }

    performLanguageCaseRuleOperation(edit_case_index, form_hash);
	}

  function moveLanguageCaseRule(case_index, position, dir) {
    var form_hash = Tr8n.Utils.serializeForm('language_cases_form');
    form_hash['case_index'] = case_index;
    form_hash['rule_index'] = position;
    form_hash['case_action'] = "move_rule_" + dir + "_at_" + position;;
    
    performLanguageCaseRuleOperation(case_index, form_hash);
  }
	
  function deleteLanguageCaseRule(case_index, position) {
    var form_hash = Tr8n.Utils.serializeForm('language_cases_form');
    form_hash['case_index'] = case_index;
    form_hash['case_action'] = "delete_rule_at_" + position;
    performLanguageCaseRuleOperation(case_index, form_hash);
  }

  function performLanguageCaseRuleOperation(case_index, params){
    Tr8n.Utils.update('language_case_rules_' + case_index, '/tr8n/language/update_language_case_rules', {
      parameters: params,
      method: 'post'
    });
  }
</script>